package com.bigshen.algorithm.eTwoPointer.solution01TwoSum;

/**
 *
 * 在 167. Two Sum II - Input array is sorted 基础上求有多少对
 *
 */
public class Another02 {

    /**
     * start指针 + end指针求和sum
     * 如果sum过大，end--
     * 如果sum过小, start++;
     * 如果相等，继续像前、向后找 start++、end--;
     * @param array
     * @param target
     * @return
     */
    public int twoSumCount(int[] array, int target) {

        if (null == array || array.length == 0) {
            return 0;
        }

        int start = 0;
        int end = array.length - 1;
        int count = 0;
        while (start < end) {
            if (array[start] + array[end] > target) {
                end--;
            } else if (array[start] + array[end] < target) {
                start++;
            } else { // 相等

                System.out.println(String.format("发现一组匹配值 array[%s] + array[%s] = %s + %s = %s",
                        start, end, array[start], array[end], target));

                count++;
                start++;
                end--;
            }
        }
        return count;
    }

    public static void main(String[] args) {

        int[] array = {1,2,3,4,5,6,7,8,9,10};
        int target = 13;
        System.out.println(new Another02().twoSumCount(array, target));
    }

}
